package priv.stones.controller.administrate;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import priv.stones.common.utils.Result;
import priv.stones.entity.SysCommitlog;
import priv.stones.service.SysCommitlogService;

/**
 * @Author: Stones
 * @Description:操作日志
 * @Date: 2017/10/24 14:39
 */
@Controller
@RequestMapping("/administrate/commitlog")
public class CommitLogController extends BaseController {

    @Autowired
    private SysCommitlogService sysCommitlogService;

    /**
     * 列表
     *
     * @param model
     * @param adminID
     * @param timeRange
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("")
    @RequiresPermissions("sys:commitlog:list")
    public String List(Model model
            , @RequestParam(value = "adminID", defaultValue = "") String adminID
            , @RequestParam(value = "timeRange", defaultValue = "") String timeRange
            , @RequestParam(value = "pageNo", defaultValue = "1") int pageNo
            , @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Wrapper wrapper = new EntityWrapper();
        if (!StringUtils.isBlank(adminID)) {
            wrapper.eq("admin", adminID);
        }
        if (!StringUtils.isBlank(timeRange)) {
            String[] times = timeRange.split(" - ");
            if (times.length == 2) {
                wrapper.ge("committime", times[0].trim()).le("committime", times[1].trim());
            }
        }
        wrapper.orderBy("committime", false);
        Page<SysCommitlog> data = sysCommitlogService.selectCustom(new Page<>(pageNo, pageSize), wrapper);
        model.addAttribute("data", data.getRecords());
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("dataCount", data.getTotal());
        model.addAttribute("timeRange", timeRange);
        model.addAttribute("adminID", adminID);
        return FreeMarkerViewResult("commitlog/list", model);
    }

    /**
     * 详细
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    @RequiresPermissions("sys:commitlog:detail")
    @ResponseBody
    public Result Detail(String id) {
        SysCommitlog data = sysCommitlogService.selectById(id);
        Result result = Result.Error();
        if (data != null) {
            data.setAdminName(sysAdminService.selectById(data.getAdmin()).getName());
            result = Result.Ok();
            result.put("data", data);
        }
        return result;
    }


}
